package com.yinhai.ta404.module.security.cas.utils;

import java.lang.management.ManagementFactory;
import java.util.Iterator;
import java.util.Set;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.Query;
import javax.naming.InitialContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ServerUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(ServerUtils.class);
    private static int PORT = 0;
    private static int HTTP_PORT = 0;
    private static int HTTPS_PORT = 0;

    public ServerUtils() {
        
    }

    public static int getPort() {
        if (PORT != 0) {
            return PORT;
        } else {
            try {
                LOGGER.debug("this application server is :{} ", ServerDetector.getServerId());
                MBeanServer server = ManagementFactory.getPlatformMBeanServer();
                if (ServerDetector.isTomcat()) {
                    Set<ObjectName> objs = server.queryNames(new ObjectName("*:type=Connector,*"), Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
                    Iterator var4 = objs.iterator();

                    while(var4.hasNext()) {
                        ObjectName obj = (ObjectName)var4.next();
                        String scheme = server.getAttribute(obj, "scheme").toString();
                        String port = obj.getKeyProperty("port");
                        if ("https".equals(scheme)) {
                            HTTPS_PORT = Integer.parseInt(port);
                            LOGGER.debug("tomcat https port:{}", port);
                        } else if ("http".equals(scheme)) {
                            HTTP_PORT = Integer.parseInt(port);
                            LOGGER.debug("tomcat http port:{}", port);
                        }
                    }
                } else {
                    ObjectName service;
                    if (ServerDetector.isWebLogic()) {
                        service = new ObjectName("com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean");
                        InitialContext ctx = new InitialContext();
                        server = (MBeanServer)ctx.lookup("java:comp/env/jmx/runtime");
                        if (server == null) {
                            server = (MBeanServer)ctx.lookup("java:comp/jmx/runtime");
                        }

                        ObjectName rt = (ObjectName)server.getAttribute(service, "ServerRuntime");
                        HTTP_PORT = Integer.parseInt(String.valueOf(server.getAttribute(rt, "ListenPort")));
                        LOGGER.debug("weblogic http port:{}", HTTP_PORT);
                        String sslEnabled = String.valueOf(server.getAttribute(rt, "SSLListenPortEnabled"));
                        if ("true".equals(sslEnabled)) {
                            HTTPS_PORT = Integer.parseInt(String.valueOf(server.getAttribute(rt, "SSLListenPort")));
                            LOGGER.debug("weblogic https port:{}", HTTPS_PORT);
                        }
                    } else if (ServerDetector.isJBoss()) {
                        try {
                            service = new ObjectName("jboss.as:socket-binding-group=standard-sockets,socket-binding=https");
                            HTTPS_PORT = Integer.parseInt(String.valueOf(server.getAttribute(service, "port")));
                            LOGGER.debug("jboss https port:{}", HTTPS_PORT);
                        } catch (Exception var6) {
                        }

                        service = new ObjectName("jboss.as:socket-binding-group=standard-sockets,socket-binding=http");
                        HTTP_PORT = Integer.parseInt(String.valueOf(server.getAttribute(service, "port")));
                        LOGGER.debug("jboss http port:{}", HTTP_PORT);
                    } else if (ServerDetector.isApusic()) {
                        service = new ObjectName("apusic:name=Muxer,j2eeType=Service");
                        String sslEnabled = String.valueOf(server.getAttribute(service, "SSLEnabled"));
                        if ("true".equals(sslEnabled)) {
                            HTTPS_PORT = Integer.parseInt(String.valueOf(server.getAttribute(service, "SecurePort")));
                            LOGGER.debug("apusic https port:{}", HTTPS_PORT);
                        }
                        HTTP_PORT = Integer.parseInt(String.valueOf(server.getAttribute(service, "Port")));
                        LOGGER.debug("apusic http port:{}", HTTP_PORT);
                    } else {
                        LOGGER.debug("Unsupport auto detect " + ServerDetector.getServerId() + " server port！Please set the rest port manually!");
                    }
                }
            } catch (Exception var7) {
                LOGGER.debug("Get application server  port fail! Please make sure that application server`s JMX is enabled!");
            }

            if (HTTPS_PORT != 0) {
                PORT = HTTPS_PORT;
            } else {
                PORT = HTTP_PORT;
            }

            return PORT;
        }
    }
}
